package main;

import java.applet.Applet;

import processing.core.PApplet;

public class Controller {
	
	private static float x=0;
	private static float y=5804;
	private static float zoom=0.5f;
	
	private static float speedX=10;
	private static float speedY=10;
	private static float zoomSpeed=0.01f;
	
	private static boolean mouseButtonPressed=false;
	private static float memoryX=0;
	private static float memoryY=0;
	
	private static boolean zoomed=false;
	private static float oldX=0;
	private static float oldY=0;
	
	public static void mouseButtonDown(PApplet applet){
		memoryX=applet.mouseX;
		memoryY=applet.mouseY;
		mouseButtonPressed=true;
	}
	
	public static void mouseButtonUp(){
		mouseButtonPressed=false;
	}
	
	public static void calculate(PApplet applet){
		if (mouseButtonPressed){
			x+=(applet.mouseX-memoryX)/zoom;
			y+=(applet.mouseY-memoryY)/zoom;
			memoryX=applet.mouseX;
			memoryY=applet.mouseY;
		}
	}
	
	public static void translate(PApplet applet){
		applet.scale(zoom);
		
		if (zoomed){
			float istX=roundTill(roundTill(applet.mouseX/zoom, 3)-roundTill(Controller.x, 3), 3);
			float istY=roundTill(roundTill(applet.mouseY/zoom, 3)-roundTill(Controller.y, 3), 3);
			x=roundTill(roundTill(x, 3)+roundTill(istX-oldX, 3), 3);
			y=roundTill(roundTill(y, 3)+roundTill(istY-oldY, 3), 3);
			zoomed=false;
		}
		
		applet.translate(x, y);
	}
	
	public static void draw(PApplet applet){
		applet.line(0, 0, Controller.x+applet.mouseX*zoom, Controller.y+applet.mouseY*zoom);
		applet.text("mouse - x:"+applet.mouseX+Controller.x+" y:"+applet.mouseY+Controller.y, (Controller.x+applet.mouseX)*zoom, (Controller.y+applet.mouseY)*zoom);
	}
	
	public static void scrollUp(){
		y+=speedY;
	}
	
	public static void scrollDown(){
		y-=speedY;
	}
	
	public static void scrollLeft(){
		x+=speedX;
	}
	
	public static void scrollRight(){
		x-=speedX;
	}
	
	public static void zoomIn(PApplet applet){
		if(zoom+zoomSpeed <= 5){
		oldX=roundTill(roundTill(applet.mouseX/zoom, 3)-roundTill(Controller.x, 3), 3);
		oldY=roundTill(roundTill(applet.mouseY/zoom, 3)-roundTill(Controller.y, 3), 3);
		zoom=roundTill(zoom+zoomSpeed, 3);
		zoomed=true;}
		translate(applet);
	}
	
	private static void calculateXY(PApplet applet){
		x=x-applet.mouseX+(scaleScreenWidth()/2);
		y=y-applet.mouseY+(scaleScreenHeight()/2);
	}
	
	public static float roundTill(float nummber, int nachKommaStelle){
		float dex=(float) Math.pow(10, nachKommaStelle);
		
		return Math.round(nummber*dex)/dex;
	}
	
	public static void zoomOut(PApplet applet){
		
		if(zoom-zoomSpeed > 0.001){
			oldX=roundTill(roundTill(applet.mouseX/zoom, 3)-roundTill(Controller.x, 3), 3);
			oldY=roundTill(roundTill(applet.mouseY/zoom, 3)-roundTill(Controller.y, 3), 3);
			zoom=roundTill(zoom-zoomSpeed, 3);
		zoomed=true;}
		translate(applet);
	}
	
	public static void zoomReset(PApplet applet){
		zoom=0.5f;
	}
	
	public static float getZoom(){
		return zoom;
	}
	
	public static void scrollReset(PApplet applet){
		x=0;
		y=5804;
	}
	
	private static float scaleScreenWidth(){
		return java.awt.Toolkit.getDefaultToolkit().getScreenSize().width*zoom;
	}
	
	private static float scaleScreenHeight(){
		return java.awt.Toolkit.getDefaultToolkit().getScreenSize().height*zoom;
	}

}
